<?php
class ControllerWxHome extends Controller{
    public function index()
    {
        require_once(DIR_SYSTEM . 'library/jssdk.php');
        $jssdk = new JSSDK("wxdc4ce97c601d543f", "f69bd9f2c11034a0fa5d4d2a527e0d9b");
        $signPackage = $jssdk->GetSignPackage();
        $accesstoken = $jssdk->getAccessToken();

        $this->load->model('wx/wx');

        if($_GET['code']) {
            //获取用户基本信息
            $code = $_GET['code'];
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxdc4ce97c601d543f&secret=f69bd9f2c11034a0fa5d4d2a527e0d9b&code=$code&grant_type=authorization_code";
            $json = file_get_contents($url);
            $arr = json_decode($json, true);
            $token = $arr['access_token'];
            $openid = $arr['openid'];
        }
            if($openid){
            //缓存openid用来抽奖
                $this->session->data['wx_openid'] = $openid;
                setcookie("wx_openid", $openid);

                //获取用户信息
                $url = "https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$openid ";
                $json = file_get_contents($url);
                $user_info = json_decode($json, true);
                //存储用户信息到DB
                $user=$this->model_wx_wx->getuserinfo($openid);
                if($user_info&&!$user){
                    $this->model_wx_wx->adduser($user_info);
                    //是否有推荐人
                    if($_GET['state']){
                        $referee_id = $_GET['state'];
                        if($referee_id!=$openid){
                            //获取该推荐人今天成功推荐的人数
                            $user_total=$this->model_wx_wx->getRefereeTotal($openid);
                            if($user_total<2){
                                $this->model_wx_wx->addLotteryTimes($referee_id);
                            }
                            $info=$this->model_wx_wx->getuserinfo($openid);
                            if(!$info['referee_id']){
                                //更新用户的推荐人
                                $this->model_wx_wx->updateUserRefereeid($openid,$referee_id);
                            }

                        }
                    }
                }

                //获取用户关注公众号的状态
                $url_subscribe_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accesstoken&openid=$openid&lang=zh_CN";
                $json_subscribe_json = file_get_contents($url_subscribe_url);
                $arr_subscribe_info= json_decode($json_subscribe_json, true);
                //更新用户关注状态
                $this->model_wx_wx->updateUserSubscribeStatus($openid,$arr_subscribe_info['subscribe']);

        }else{
            $openid='';
        }

        $this->data['share_action']=$this->url->link('wx/home/share', '', 'SSL');
        $this->data['user_name']=$user_info['nickname'];
        $this->data['openid']=$openid;
        $this->data['signPackage']=$signPackage;



        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/wx/home.tpl';
        } else {
            $this->template = 'default/template/wx/home.tpl';
        }


        $this->response->setOutput($this->render());

    }
    public function share(){
        $this->load->model('wx/wx');
        $open_id=$this->request->post['openid'];
        $this->model_wx_wx->share($open_id);
    }
}
?>